package com.plian.system.service.wf;

import com.plian.Tools.pagedata.PageData;
import com.plian.system.bean.wf.WFExpressionProperty;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.constant.wf.WFExpressionPropertyConstant;
import com.plian.system.model.ObjResult;
import org.flowable.ui.modeler.dao.WFExpressionPropertyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class WFExpressionPropertyServiceImpl implements WFExpressionPropertyService {
    private final WFExpressionPropertyMapper wfExpressionPropertyMapper;

    @Autowired
    public WFExpressionPropertyServiceImpl(WFExpressionPropertyMapper wfExpressionPropertyMapper) {
        this.wfExpressionPropertyMapper = wfExpressionPropertyMapper;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional
    public void addByBean(WFExpressionProperty wfExpressionProperty){
        if (!WFExpressionPropertyConstant.INPUT.contains(wfExpressionProperty.getInput())) {
            throw new MyRuntimeException(new ObjResult("非法的输入框类型"));
        }
        if (!WFExpressionPropertyConstant.TYPE.contains(wfExpressionProperty.getType())) {
            throw new MyRuntimeException(new ObjResult("非法的值类型"));
        }
        if (!WFExpressionPropertyConstant.OPERATOR.containsAll(Arrays.asList(wfExpressionProperty.getOperators().split(",")))) {
            throw new MyRuntimeException(new ObjResult("非法的操作符"));
        }
        wfExpressionPropertyMapper.insert(wfExpressionProperty);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<WFExpressionProperty> getAll() {
        return wfExpressionPropertyMapper.getAll();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public PageData getById(String id) {
        List<PageData> wfExpressionProperty = wfExpressionPropertyMapper.getById(id);
        return wfExpressionProperty.get(0);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional
    public void updateByBean(PageData pd) {
        if (!WFExpressionPropertyConstant.INPUT.contains(pd.getString("input"))) {
            throw new MyRuntimeException(new ObjResult("非法的输入框类型"));
        }
        if (!WFExpressionPropertyConstant.TYPE.contains(pd.getString("type"))) {
            throw new MyRuntimeException(new ObjResult("非法的值类型"));
        }
        if (!WFExpressionPropertyConstant.OPERATOR.containsAll(Arrays.asList(pd.getString("operators").split(",")))) {
            throw new MyRuntimeException(new ObjResult("非法的操作符"));
        }
        wfExpressionPropertyMapper.updateByPagedata(pd);
    }

    ///**
    // * {@inheritDoc}
    // */
    //@Override
    //@Transactional
    //public void deleteByBean(WFExpressionProperty wfExpressionProperty) throws LogicalException {
    //    deleteById(wfExpressionProperty.getId());
    //}
    //
    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional
    public void deleteById(String id){
        wfExpressionPropertyMapper.deleteById(id);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional
    public void deleteByIdBatch(List<String> ids){
        wfExpressionPropertyMapper.deleteByIds(ids);
    }

    ///**
    // * {@inheritDoc}
    // */
    //@Override
    //public void isPresentById(String id) throws LogicalException {
    //    WFExpressionPropertyExample example = new WFExpressionPropertyExample();
    //    example.createCriteria()
    //            .andIdEqualTo(id)
    //            .andIsDeleteEqualTo(0);
    //    if (wfExpressionPropertyMapper.countByExample(example) != 1) {
    //        throw new LogicalException("未找到表达式属性" + id + "或无权访问");
    //    }
    //}
    //
    ///**
    // * {@inheritDoc}
    // */
    //@Override
    //public void isPresentByIds(List<String> ids) throws LogicalException {
    //    WFExpressionPropertyExample example = new WFExpressionPropertyExample();
    //    example.createCriteria()
    //            .andIdIn(ids)
    //            .andIsDeleteEqualTo(0);
    //    if (wfExpressionPropertyMapper.countByExample(example) != ids.size()) {
    //        throw new LogicalException("部分表达式属性未找到或无权访问");
    //    }
    //}
}
